package com.yuliang.vueblog.controller;

import cn.hutool.core.map.MapUtil;
import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import com.yuliang.vueblog.common.LoginDto;
import com.yuliang.vueblog.common.Result;
import com.yuliang.vueblog.entity.User;
import com.yuliang.vueblog.service.UserService;
import com.yuliang.vueblog.util.JwtUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;

@RestController
public class AccountController {

    @Autowired
    UserService userService;

    @Autowired
    JwtUtils jwtUtils;

    @PostMapping("/login")
    public Result login(@Validated @RequestBody LoginDto user, HttpServletResponse response){
//        Subject subject = SecurityUtils.getSubject();
//        UsernamePasswordToken token = new UsernamePasswordToken(user.getUsername(), user.getPassword());
//
//        try{
//            subject.login(token);
//            User userInfo = (User)subject.getPrincipal();
//            String jwt = jwtUtils.generateToken(userInfo.getId());
//            response.setHeader("Authorization", jwt);
//            response.setHeader("Access-Control-Expose-Headers", "Authorization");
//            // 用户可以另一个接口
//            return Result.succ(MapUtil.builder()
//                    .put("id", userInfo.getId())
//                    .put("username", userInfo.getUsername())
//                    .put("avatar", userInfo.getAvatar())
//                    .put("email", userInfo.getEmail())
//                    .map()
//            );
//        }catch (UnknownAccountException e){
//            return Result.fail("账号不存在");
//        }

        User userInfo = userService.getOne(new QueryWrapper<User>().eq("username", user.getUsername()));
        Assert.notNull(userInfo,"用户不存在");
        if (!userInfo.getPassword().equals(SecureUtil.md5(user.getPassword()))){
            return Result.fail("密码不正确");
        }
        String jwt = jwtUtils.generateToken(userInfo.getId());
        response.setHeader("Authorization", jwt);
        response.setHeader("Access-Control-Expose-Headers", "Authorization");
        // 用户可以另一个接口
        return Result.succ(MapUtil.builder()
                .put("id", userInfo.getId())
                .put("username", userInfo.getUsername())
                .put("avatar", userInfo.getAvatar())
                .put("email", userInfo.getEmail())
                .map()
        );
    }

    // 退出
    @GetMapping("/logout")
    @RequiresAuthentication
    public Result logout() {
        SecurityUtils.getSubject().logout();
        return Result.succ(null);
    }
}
